package com.googlecode.jwsm.security.bogus;

import java.io.*;

import javax.servlet.http.*;

import com.googlecode.jwsm.security.SecurityFactory;
import com.googlecode.jwsm.security.SuperUser;
import com.googlecode.jwsm.security.User;

public class BogusSecurityFactory implements SecurityFactory {
	public Object getApplicationProperty(String arg0) {
		return null;
	}

	public User login(String username, String password) {
		return new SuperUser(username);
	}

	public User login(HttpServletRequest request, HttpServletResponse response) {
		if (request.getSession().getAttribute("superUser") != null)
			return (User) request.getSession().getAttribute("superUser");
		User user = null;
		if (request.getParameter("username") != null) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			user = login(username, password);
		}
		request.getSession().setAttribute("superUser", user);
		return user;
	}

	public User login(HttpServletRequest request, HttpServletResponse response, String formURL) {
		User user = null;
		try {
			user = login(request, response);
		} catch (SecurityException exc) {
			exc.printStackTrace();
		}
		if (user == null && formURL != null) try {
			response.sendRedirect(formURL);
		} catch (IOException exc) {
			throw new SecurityException("Problem occurred trying to redirect to form based login: " + formURL);
		}
		return user;
	}

	public void logout(HttpServletRequest request, HttpServletResponse response, String arg2) {
		request.getSession().removeAttribute("superUser");
		try {
			response.sendRedirect(request.getRequestURL().toString());
		} catch (IOException exc) {
			throw new SecurityException(exc.getMessage());
		} catch (IllegalStateException exc) {
		}
	}

	public void setApplicationProperty(String arg0, Serializable arg1) {
	}
}
